package cn.edu.cqut.mdm.core.module;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.cqut.mdm.core.module.PublishStatus.Status;
import cn.edu.cqut.mdm.core.publisherdispatcher.LocalPublisherDispatcher;

public class ModuleEntity {
	private String uid;
	private String name;
	private Class<?> moduleClazz;
	private List<Method> methods;
	private Map<String, PublishStatus> publishStatuses = new HashMap<String, PublishStatus>();
	
	public ModuleEntity(String uid, String name, Class<?> clazz, List<Method> methods) {
		this.uid = uid;
		this.name = name;
		this.moduleClazz = clazz;
		this.methods = methods;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Class<?> getModuleClazz() {
		return moduleClazz;
	}

	public void setModuleClazz(Class<?> clazz) {
		this.moduleClazz = clazz;
	}

	public List<Method> getMethods() {
		return methods;
	}

	public void setMethods(List<Method> methods) {
		this.methods = methods;
	}
	
	public RemoteModuleEntity toRemoteModuleEntity(String publisherDispatcherName) {
		RemoteModuleEntity remoteModuleEntity = new RemoteModuleEntity();
		List<RemoteMethod> methods = new ArrayList<RemoteMethod>();
		for(Method method : this.getMethods()) {
			methods.add(RemoteMethod.fromMethod(method));
		}
		remoteModuleEntity.setUid(this.getUid());
		remoteModuleEntity.setMethods(methods);
		remoteModuleEntity.setName(this.getName());
		remoteModuleEntity.setPublisherDispatcherName(publisherDispatcherName);
//		for(PublishStatus publishStatus : this.publishStatuses.values()) {
//			if(!publishStatus.getPublisherDispatcher().getName().equals(new LocalPublisherDispatcher().getName()) &&
//					publishStatus.getStatus() == Status.PUBLISHED) {
//					remoteModuleEntity.setPublisherDispatcherName(publishStatus.getPublisherDispatcher().getName());
//			}
//		}
		return remoteModuleEntity;
	}

	public PublishStatus getPublishStatus(String publisherName) {
		return this.publishStatuses.get(publisherName);
	}
	
	public void addPublishStatus(PublishStatus publishStatus) {
		this.publishStatuses.put(publishStatus.getPublisherDispatcher().getName(), publishStatus);
	}
	public Map<String, PublishStatus> getAllPublishStatusMap() {
		return this.publishStatuses;
	}
	
}
